home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / graphics.17 / graphics / graphics-0.17 / dist-stat / rank.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-14  |  3.1 KB  |  171 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/rank.c,v 1.2 90/09/04 17:40:46 toy Exp $
  3.  * NAME
  4.  *    rank - rank (number of elements) of a vector
  5.  *
  6.  * SYNOPSIS
  7.  *    rank [-H] [-P prec] [-c cols] [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is the rank of the the specified vector(s).
  11.  *
  12.  * HISTORY
  13.  * $Log:    rank.c,v $
  14.  * Revision 1.2  90/09/04  17:40:46  toy
  15.  * Set default precision from the environment variable STAT_PREC.
  16.  * Use print_help_strings to print out help.
  17.  *
  18.  * Revision 1.1  90/09/01  17:52:10  toy
  19.  * Initial revision
  20.  *
  21.  * Revision 1.2  90/09/01  17:01:38  toy
  22.  * Use message function to print out messages.
  23.  *
  24.  * Revision 1.1  90/09/01  14:07:22  toy
  25.  * Initial revision
  26.  *
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <string.h>
  33.  
  34. #if    defined(__STDC__) || defined(__GNUC__)
  35. #include <stddef.h>
  36. #include <stdlib.h>
  37. #endif
  38.  
  39. #include "gps.h"
  40.  
  41. #define    DEF_PROGNAME    "rank"
  42.  
  43. static const char RCSID[] = "@(#) $Id: rank.c,v 1.2 90/09/04 17:40:46 toy Exp $";
  44.  
  45. extern int getopt ();
  46. extern int optind;
  47. extern char *optarg;
  48.  
  49. const char *progname;
  50.  
  51. #define    OPT_STRING    "HP:c:"
  52.  
  53. void
  54. help ()
  55. {
  56.   static const char *help_strings[] =
  57.   {
  58.     "\t-H\tThis help\n",
  59.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  60.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  61.     "\t\tuse default.\n",
  62.     "\t-c c\tSet number of output elements per line to n.  (Default = 1.)\n",
  63.     "The rank (number of elements) of the vector is printed.\n",
  64.     "If more than one vector is given, the rank of each vector\n",
  65.     "is printed.\n",
  66.     NULL
  67.   };
  68.   (void) fprintf (stderr, "%s\n", RCSID);
  69.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [vector ...]\n", progname);
  70.   print_help_strings (help_strings);
  71. }
  72.  
  73.  
  74.  
  75. #if    defined(__STDC__) || defined(__GNUC__)
  76. void
  77. do_func (FILE * fp)
  78. #else
  79. void
  80. do_func (fp)
  81.      FILE *fp;
  82. #endif
  83. {
  84.   int rc;
  85.   long rank;
  86.   double x;
  87.  
  88.   rank = 0;
  89.   do
  90.     {
  91.       rc = get_number (fp, &x);
  92.       if (rc == 1)
  93.     {
  94.       rank++;
  95.     }
  96.       else if (rc == 0)
  97.     {
  98.       message ("Error reading number\n");
  99.     }
  100.   } while (rc == 1);
  101.  
  102.   print_number (stdout, (double) rank);
  103. }
  104.  
  105.  
  106. int
  107. main (argc, argv)
  108.      int argc;
  109.      char *argv[];
  110. {
  111.   int option;
  112.   double total;
  113.  
  114.   progname = get_my_name (argv[0], DEF_PROGNAME);
  115.   set_def_prec ();
  116.  
  117.   /*
  118.    * Read options
  119.    */
  120.  
  121.   (void) set_max_columns (1);
  122.  
  123.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  124.     {
  125.       switch (option)
  126.     {
  127.     case 'H':        /* Help                 */
  128.       help ();
  129.       exit (0);
  130.       break;
  131.     case 'P':        /* Precision             */
  132.       (void) set_precision (atoi (optarg));
  133.       break;
  134.     case 'c':        /* Number of columns         */
  135.       (void) set_max_columns (atoi (optarg));
  136.       break;
  137.     default:
  138.       break;
  139.     }            /* endswitch     */
  140.     }                /* endwhile     */
  141.  
  142.   /*
  143.    * If no files are listed, use stdin.  Otherwise use
  144.    * the given files
  145.    */
  146.  
  147.   if (optind == argc)
  148.     {
  149.       do_func (stdin);
  150.     }
  151.   else
  152.     {
  153.       total = 0;
  154.       while (optind < argc)
  155.     {
  156.       if (freopen (argv[optind], "r", stdin) == NULL)
  157.         {
  158.           message ("Cannot open `%s'\n", argv[optind]);
  159.         }
  160.       else
  161.         {
  162.           do_func (stdin);
  163.         }
  164.       optind++;
  165.     }            /* endwhile     */
  166.     }                /* endif     */
  167.  
  168.   end_column (stdout);
  169.   return 0;
  170. }
  171.